package com.nowcoder;

import java.util.Scanner;

/**
 *判断两个IP是否属于同一子网
 */
public class IPSubnet {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String mask = in.nextLine();
            String ip1 = in.nextLine();
            String ip2 = in.nextLine();
            String[] masknums = mask.split("\\.");
            String[] ipnums1 = ip1.split("\\.");
            String[] ipnums2 = ip2.split("\\.");
            boolean error = false, subnet = true;
            StringBuilder maskbinarys = new StringBuilder();
            if (masknums.length != 4 || ipnums1.length != 4 || ipnums2.length != 4) {
                error = true;
                continue;
            }
            for (int i = 0; i < 4; i++) {
                int ip1num = Integer.parseInt(ipnums1[i]);
                if(ip1num < 0 || ip1num > 255){
                    error = true;
                    break;
                }
                int ip2num = Integer.parseInt(ipnums2[i]);
                if(ip2num < 0 || ip2num > 255){
                    error = true;
                    break;
                }
                int masknum = Integer.parseInt(masknums[i]);
                if(masknum < 0 || masknum > 255){
                    error = true;
                    break;
                }
                if ((ip1num & masknum) != (ip2num & masknum)) {
                    subnet = false;
                }
                String binary = Integer.toBinaryString(masknum);
                while(binary.length()<8){
                    binary = "0" + binary;
                }
                maskbinarys.append(binary);
            }
            if (error || maskbinarys.lastIndexOf("1") > maskbinarys.indexOf("0")) {
                System.out.println(1);
            } else if (!subnet) {
                System.out.println(2);
            } else {
                System.out.println(0);
            }
        }
    }
}
